<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">
            <!-- 放置表单 -->
            <el-form ref="form" :model="form" :rules="rules" label-width="120px" style="margin-left: 120px; margin-top:30px">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="form.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="password">
                <el-input v-model="form.password" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="saveUserDetail">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <el-tooltip class="tooltip-box" content="打印基本个人信息">
              <router-link :to="`/employees/print/${userId}`">
                <i class="el-icon-printer" />
              </router-link>
            </el-tooltip>
            <UserInfo />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <JobInfo />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import UserInfo from './components/UserInfo.vue'
import JobInfo from './components/JobInfo.vue'
import { getUserDetail } from '@/api/user'
import { saveUserDetailById } from '@/api/employees'
export default {
  components: {
    UserInfo,
    JobInfo
  },
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: ['change', 'blur'] }
        ],
        newPassword: [
          { required: true, message: '密码不能为空', trigger: ['blur'] },
          { min: 6, max: 9, message: '密码长度6-9位', trigger: ['change', 'blur'] }
        ]
      }
    }
  },
  computed: {
    userId() {
      return this.$route.params.id
    }
  },
  created() {
    this.getUserDetail()
  },
  methods: {
    async getUserDetail() {
      // 1. 路由中的传过来的id
      const res = await getUserDetail(this.userId)
      //   console.log(res)
      this.form = res
    },
    // 2.保存员工基本信息
    async saveUserDetail() {
      try {
        await this.$refs.form.validate()
      } catch {
        return
      }
      // 发送保存请求
      await saveUserDetailById({ ...this.form, id: this.userId })
      this.$message.success('更新信息成功')
    }
  }
}
</script>

<style lang="scss" scoped>
// .employees-detail-container {
.app-container {
  .el-tab-pane {
    padding-top: 10px;
  }
  .tooltip-box {
    position: absolute;
    right: 30px;
    top: 10px;
    z-index: 999;
  }
}
</style>
